<?php
	include_once("DataProvider.php");
	include_once("../DTO/taiKhoanDTO.php");
	class TaiKhoanDAO
	{
		public $sql="";
		var $taiKhoanDTO;
		function __construct()
		{
			return $this->taiKhoanDTO = new taiKhoanDTO();
		}
		
		public function load()
		{
			$result= DataProvider::ExecuteQuery($this->sql);
			$arrTaiKhoanDTO[]= new ArrayObject(); 
			
			while($row = mysql_fetch_array($result))
			{
				$taiKhoanDTO = new taiKhoanDTO();
				
				$taiKhoanDTO->idTaiKhoan =$row['idTaiKhoan']; 
				$taiKhoanDTO->tenDangNhap =$row['tenDangNhap']; 
				$taiKhoanDTO->matKhau =$row['matKhau']; 
				$taiKhoanDTO->hoTen =$row['hoTen']; 
				$taiKhoanDTO->cmnd =$row['cmnd']; 
				$taiKhoanDTO->sdt =$row['sdt']; 
				$taiKhoanDTO->email =$row['email']; 
				$taiKhoanDTO->diaChi =$row['diaChi']; 
				$taiKhoanDTO->biXoa =$row['biXoa'];
				$taiKhoanDTO->idLoaiTaiKhoan =$row['idLoaiTaiKhoan']; 
				$taiKhoanDTO->soDuTaiKhoan =$row['soDuTaiKhoan']; 
				$taiKhoanDTO->ngay =$row['ngay'];
				$taiKhoanDTO->biKhoa = $row['biKhoa']; 
				
				$arrTaiKhoanDTO[]= $taiKhoanDTO;
			}
			return $arrTaiKhoanDTO;
		}
		
		public function load_All()
		{
			$this->sql= "select * from taiKhoan";
			$result=$this->load();
			return $result; 
		}
		
		public function load_One($idTaiKhoan)
		{
			$this->sql= "select * from taiKhoan where idTaiKhoan=".$idTaiKhoan;
			$result=$this->load();
			return $result; 
		}
		
		 function kiemTraTaiKhoan()
        {
			if (isset($_POST['txtUsername']))
			{
				$username = $_POST['txtUsername'];
                $email = $_POST['txtemail'];
			}
			else
			{
				$username = $_POST['txtUser'];
                $email = $_POST['txtemail'];
			}  
            $sql = "select * from taiKhoan where tenDangNhap='$username' or email='$email'";
            $result = DataProvider::ExecuteQuery($sql);
            $num = mysql_num_rows($result);
            return $num;           
        }
		
		
		
		function themTaiKhoan()
        {
			
            $tenDangNhap = $_POST['txtUsername'];
            $matKhau = $_POST['txtPassword'];
            $hoTen = $_POST['txthoten'];
            $cmnd = $_POST['txtcmnd'];
            $sdt = $_POST['txtsdt'];
            $email = $_POST['txtemail'];
            $diaChi = $_POST['txtdiachi'];
            $idLoaiTK = $_POST['cmbLoaiTK'];
			$ngay = $currentDate = date("Y-m-d");            
            $sql = "insert into taikhoan(idTaiKhoan,tenDangNhap, matKhau, hoTen, cmnd, sdt, email, diaChi, biXoa, idLoaiTaiKhoan, soDuTaiKhoan, ngay, biKhoa) values(NULL, '$tenDangNhap', '$matKhau', '$hoTen', '$cmnd', '$sdt', '$email', '$diaChi', 0, '$idLoaiTK', 0, '$ngay', 0)";
            $result = DataProvider::ExecuteQuery($sql);
            return $result;
        }
		
		function xoaTaiKhoan()
        {
            $idTK = $_REQUEST['idTaiKhoan'];
            $sql = "update taiKhoan set biXoa=1 where idTaiKhoan=$idTK";
            $result = DataProvider::ExecuteQuery($sql);
            return $result;
        }
		
		function capNhatTaiKhoan()
        {
            $hoTen = $_POST['txtHoTen'];
            $cmnd = $_POST['txtCMND'];
            $sdt = $_POST['txtSDT'];
            $email = $_POST['txtEmail'];
            $diaChi = $_POST['txtDiaChi'];
			$id = $_SESSION["id"];
                        
            $sql = "update taiKhoan set hoTen = '$hoten', cmnd = '$cmnd', sdt = '$sdt', email = '$email', diaChi = '$diaChi'  where idTaiKhoan = $id";
            $result = DataProvider::ExecuteQuery($sql);
            return $result;
        }
		
		function capNhatMatKhau()
		{
			$id = $_SESSION["id"];
			$matKhau = $_POST['txtMatKhau'];
			
			$sql = "update taiKhoan set matKhau = '$matKhau'  where idTaiKhoan = $id";
            $result = DataProvider::ExecuteQuery($sql);
            return $result;
			
		}
		
		function capNhatLoaiTK()
		{
			$id = $_SESSION["id"];
			$idLoaiTK = $_POST['rdbIDLoaiTaiKhoan'];
			
			$sql = "update taiKhoan set idLoaiTaiKHoan=$idLoaiTK  where idTaiKhoan = $id";
            $result = DataProvider::ExecuteQuery($sql);
            return $result;
		}
		
		function timTaiKhoan()
        {
            if(isset($_REQUEST['btnTimKiem']))
            {
                $tim = $_REQUEST['txtTenDangNhap'];
                $sql = "select * from taiKhoan where tenDangNhap like '%".$tim."%'";
                $arrTaiKhoan = new ArrayObject();
                $result = DataProvider::ExecuteQuery($sql);
                if($result != false)
                {            
                    if(mysql_num_rows($result)>0)
                    {
                        while($row = mysql_fetch_array($result))
                        {
                            $taiKhoanDTO = new taiKhoanDTO();
							$taiKhoanDTO->idTaiKhoan =$row['idTaiKhoan']; 
							$taiKhoanDTO->tenDangNhap =$row['tenDangNhap']; 
							$taiKhoanDTO->matKhau =$row['matKhau']; 
							$taiKhoanDTO->hoTen =$row['hoTen']; 
							$taiKhoanDTO->cmnd =$row['cmnd']; 
							$taiKhoanDTO->sdt =$row['sdt']; 
							$taiKhoanDTO->email =$row['email']; 
							$taiKhoanDTO->diaChi =$row['diaChi']; 
							$taiKhoanDTO->biXoa =$row['biXoa'];
							$taiKhoanDTO->idLoaiTaiKhoan =$row['idLoaiTaiKhoan']; 
							$taiKhoanDTO->soDuTaiKhoan =$row['soDuTaiKhoan']; 
							$taiKhoanDTO->kichHoat =$row['kichHoat']; 
                            $arrTaiKhoan[] = $TaiKhoanDTO;
                        }
                        return $arrTaiKhoan;                   
                    }
                }

            }
		}
		
		
		
		
		public function xuLyDangNhap()
		{
			if(isset($_POST['txtUsername']))
			{
				$username = $_POST["txtUsername"];
				$password = $_POST["txtPassword"];
			}
			else
			{
				$username = $_POST["txtUser"];
				$password = $_POST["txtPass"];
			}
			
			$sql = "select * from taiKhoan where tenDangNhap = '". $username ."' and matKhau = '".$password."'";
			$result = DataProvider::ExecuteQuery($sql);
			$num = mysql_num_rows($result);
			return $num;
		}
		
		public function taoSessionUser()
		{
			if(isset($_POST['txtUsername']))
			{
				$username = $_POST["txtUsername"];
				$password = $_POST["txtPassword"];
			}
			else
			{
				$username = $_POST["txtUser"];
				$password = $_POST["txtPass"];
			}
			$sql = "select * from taiKhoan where tenDangNhap = '". $username ."' and matKhau = '".$password."'";
			$result = DataProvider::ExecuteQuery($sql);
			$member = mysql_fetch_array($result);
			$_SESSION["id"] = $member["idTaiKhoan"];
			return $_SESSION["id"];
		}
		
		function layTenDangNhap()
		{
			$id = $_SESSION["id"];
			$sql = "select * from taiKhoan where idTaiKhoan = ".$id;
			$result = DataProvider::ExecuteQuery($sql);
			$arrTaiKhoan = new ArrayObject();
			$row = mysql_fetch_array($result);
			$TaiKhoanDTO = new taiKhoanDTO();
			$TaiKhoanDTO->tenDangNhap = $row["tenDangNhap"];
			$arrTaiKhoan[] = $TaiKhoanDTO;
			return $arrTaiKhoan;
		}
		
		function dangXuat()
		{
			return session_destroy();
		}
	}
?>
